# == Schema Information
# Schema version: 22
#
# Table name: jobs
#
#  id          :integer       not null, primary key
#  institution :string(255)
#  position    :string(255)
#  department  :string(255)
#  start_date  :date
#  end_date    :date
#  city        :string(255)
#  state       :string(255)
#  user_id     :integer
#

class Job < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :position, :institution, :start_date, :city, :state, :user
  validates_date :start_date, :end_date, :allow_nil => true
  attr_accessor :start_year, :end_year, :start_month, :end_month
  before_validation :create_dates_from_years

  private

  def create_dates_from_years
    self.start_date = Date.new(self.start_year.to_i, self.start_month.to_i) unless self.start_year.blank? or self.start_month.blank?
    self.end_date = (self.end_year.blank? or self.end_month.blank?) ? nil : Date.new(self.end_year.to_i, self.end_month.to_i)
  end

end
